export default {
	// 全局配置
	common:{
		baseUrl:'http://39.98.171.167:8888',
		// baseUrl:'http://192.168.0.83:8888',
		header:{
			'Content-Type':'application/json;charset=UTF-8'
		},
		data:{},
		method:'GET',
		dataType:'json'
	},
	request(options={}){
		// 请求 返回promise
		// 组织参数
		options.url = this.common.baseUrl + options.url;
		options.header = options.header || this.common.header;
		options.data = options.data || this.common.data;
		options.method = options.method || this.common.method;
		options.dataType = options.dataType || this.common.dataType;
		let sessionKey = uni.getStorageSync('sessionKey')
		if(sessionKey){
			options.url += `?sessionKey=${sessionKey}`
		}
		// 请求
		return new Promise((res,rej)=>{
			// 请求之前... todo
			// 请求中...
			uni.request({
				...options,
				success: (result) => {
					uni.hideLoading()
					// 服务端失败
					if(result.statusCode !== 200){
						uni.showToast({
							title: result.data.msg || '服务端失败',
							icon: 'none'
						});
						return rej(result.data) 
					}
					 switch(result.data.statusCode){
						 case '200':
						 return res(result.data);
						 break;
						 case '401':
						 uni.showToast({
						 	title:'请先登录',
						 	icon: 'none'
						 });
						 setTimeout(()=>{
						 	uni.redirectTo({
						 		url:'/pages/login/login'
						 	})
						 },300) 
						 break;
						 default:
						 uni.showToast({
						 	title: result.data.desc || '请求报错',
						 	icon: 'none'
						 });
						 return rej(result.data)
						 break;
					 }
				},
				fail: (error) => {
					uni.hideLoading()
					console.log('error msg',error)
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej(error)
				}
			});
		})
	},
	//图片上传
	imageUpload(url,filePath,formData,options = {}){
		options.url = this.common.baseUrl +url;
		options.filePath = filePath;
		options.formData =  options.formData || {};
		options.name = options.name||'file';
		let sessionKey = uni.getStorageSync('sessionKey')
		if(sessionKey){
			options.url += `?sessionKey=${sessionKey}`
		}
		console.log(options)
		return new Promise((res,rej) =>{
			const uploadTask= uni.uploadFile({
				...options,
				success: (result)=>{
					uni.hideLoading()
					if(result.statusCode===200){
						const data= JSON.parse(result.data)
						if(data.statusCode!=='200'){
							uni.showToast({
								title: data.msg || '请求报错',
								icon: 'none'
							});
							return rej(data) 
						}else{
							return res(data)
						}
						
					}else{
						uni.showToast({
							title: result.errMsg || '请求报错',
							icon: 'none'
						});
						return rej(result.data) 
					}
				},
				fail:(error)=>{
					console.log('error msg',error)
					uni.showToast({
						title: error.errMsg || '上传失败',
						icon: 'none'
					});
					return rej(error)
				}
			})
			uploadTask.onProgressUpdate((res) =>{
				if(res.progress<100){
					uni.showLoading({
					    title: '正在上传...'
					});
				}else{
					uni.hideLoading()
				}
			})
		})
	},
	// Get请求
	get(url,data = {},options = {}){
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url,data={},options={}){
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	}
}